/**
 * 缓存
 * */
layui.define(['globalConfig'], function(exports) {

	let globalConfig = layui.globalConfig;
	let sdLocal = {
		// 临时数据缓存
		putTempData: function(key, val) {
			if (val != undefined && val != null) {
				layui.sessionData(globalConfig.localTempKey, {
					key: key,
					value: val
				})
			} else {
				layui.sessionData(globalConfig.localTempKey, {
					key: key,
					remove: true
				})
			}
		},
		getTempData: function(key) {
			let data = layui.sessionData("tempData");
			if (data) {
				return data[key]
			} else {
				return false
			}
		},
		// 退出登录
		loginOut: function() {
			sdLocal.clean();
			return location.replace('/login.html');
		},
		// 清空缓存
		clean: function() {
			sdLocal.token.remove();
			sdLocal.user.put();
			sdLocal.app.put();
			sdLocal.menus.put();
		},
		// 读取本地缓存
		session: function() {
			return layui.data(globalConfig.localCacheKey);
		},
		// 菜单缓存处理
		menus: {
			put: function(menus) {
				sdLocal.set("menus", menus);
			},
			get: function() {
				return sdLocal.get('menus');
			}
		},
		// token处理
		token: {
			get: function() {
				return sdLocal.get('token');
			},
			remove: function() {
				sdLocal.set("token");
			},
			put: function(token) {
				sdLocal.set("token", token);
			}
		},
		// 子系统处理
		app: {
			put: function(app) {
				sdLocal.set("app", app);
			},
			get: function() {
				return sdLocal.get('app');
			}
		},
		// 用户处理
		user: {
			get: function() {
				return sdLocal.get('user');
			},
			put: function(user) {
				sdLocal.set("user", user);
			}
		},
		info: {
			get: function() {
				return sdLocal.get('info');
			},
			put: function(info) {
				sdLocal.set("info", info);
			}
		},
		// 非临时数据缓存
		set: function(key, val) {
			if (val != undefined && val != null) {
				let ok = layui.data(globalConfig.localCacheKey, {
					key: key,
					value: val
				})
			} else {
				layui.data(globalConfig.localCacheKey, {
					key: key,
					remove: true
				})
			}
		},
		get: function(key) {
			let session = sdLocal.session();
			if (session) {
				return session[key]
			} else {
				return null;
			}
		}
	}
	exports('sdLocal', sdLocal);
})
